//
//  HFWeekMenuView.m
//  HFCalendar
//
//  Created by Luffy on 2021/8/24.
//

#import "HFWeekMenuView.h"
#import "HFCalendarHeader.h"
#import "HFCalendarHelper.h"

@interface HFWeekMenuView ()

@property (nonatomic, strong) HFCalendarHelper *helper;
@property (nonatomic, assign) CGFloat lineView_height;

@end

@implementation HFWeekMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.helper = [HFCalendarHelper defaultManager];
        self.lineView_height = 0.4f;

        [self setupUI];
    }
    return self;
}

#pragma mark - Intial Methods
- (void)setupUI {
    self.backgroundColor = UIColor.whiteColor;

    NSDateFormatter *formatter = [self createDateFormatter];
    NSMutableArray *days = [[formatter standaloneWeekdaySymbols] mutableCopy];
    
    [days enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bounds.size.width / days.count * idx, 0, self.bounds.size.width / days.count, self.bounds.size.height - self.lineView_height)];
        weekdayLabel.text = [self theWeek:obj];
        weekdayLabel.font = [UIFont boldSystemFontOfSize:14.f];
        weekdayLabel.textAlignment = NSTextAlignmentCenter;
        weekdayLabel.textColor = CustomTitleColor;
        
        if (idx == 0 || idx == days.count - 1) {
            weekdayLabel.textColor = CustomRedColor;
        }
        
        [self addSubview:weekdayLabel];
    }];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - self.lineView_height, self.bounds.size.width, self.lineView_height)];
    lineView.backgroundColor = CustomLineColor;
    [self addSubview:lineView];
}

- (NSDateFormatter *)createDateFormatter {
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    
    dateFormatter.timeZone = self.helper.calendar.timeZone;
    dateFormatter.locale = self.helper.calendar.locale;
    
    return dateFormatter;
}

- (NSString *)theWeek:(NSString *)theWeek {
    NSString *Str;
    
    if(theWeek) {
        if([theWeek isEqualToString:@"Monday"] || [theWeek isEqualToString:@"星期一"]) {
            Str = @"一";
        } else if([theWeek isEqualToString:@"Tuesday"] || [theWeek isEqualToString:@"星期二"]) {
            Str = @"二";
        } else if([theWeek isEqualToString:@"Wednesday"] || [theWeek isEqualToString:@"星期三"]) {
            Str = @"三";
        } else if([theWeek isEqualToString:@"Thursday"] || [theWeek isEqualToString:@"星期四"]) {
            Str = @"四";
        } else if([theWeek isEqualToString:@"Friday"] || [theWeek isEqualToString:@"星期五"]) {
            Str = @"五";
        } else if([theWeek isEqualToString:@"Saturday"] || [theWeek isEqualToString:@"星期六"]) {
            Str = @"六";
        } else if([theWeek isEqualToString:@"Sunday"] || [theWeek isEqualToString:@"星期日"]) {
            Str = @"日";
        }
    }
    return Str;
}

@end
